/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package liftsimulator;

import Interface.AdminUI;
import Interface.SimUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 *
 * @author Michael Peever & Alex Pool
 */
public class LiftSimulator implements ActionListener {

    public int noLifts;
    public int noFloors;
    public String region;
    public String liftColour;
    public String liftShaftColour;
    AdminUI aui;
    SimUI sui;
    Building b;
    public int counter = 0;
    FloorUI fui;

    public LiftSimulator() {
        createAdminUI();
    }

    public void createAdminUI() {
        aui = new AdminUI();
        aui.applyButton.addActionListener(this);
    }

    public void setupSim() {
        sui = new SimUI();
        sui.runButton.addActionListener(this);
        b = new Building(this.noLifts, this.noFloors, this.region, this.liftColour, this.liftShaftColour);


    }

    public void createFloorUI() {
        if (fui != null) {
            fui.dispose();
        }

        fui = new FloorUI(this.noFloors, b.ls.e, b);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("Apply")) {
            if (counter < 1) {
                this.noLifts = aui.getNoLifts();
                this.noFloors = aui.getNoFloors();
                this.region = aui.getRegion();
                this.liftColour = aui.getLiftColour();
                this.liftShaftColour = aui.getLiftShaftColour();

                //create ElevatorUI windows for each elevator


                if (sui != null) {
                    sui.dispose();
                }
                if (b != null) {
                    b.dispose();
                }

                if (fui != null) {
                    fui.dispose();
                }
            } else {
                this.noLifts = aui.getNoLifts();
                this.noFloors = aui.getNoFloors();
                this.region = aui.getRegion();
                this.liftColour = aui.getLiftColour();
                this.liftShaftColour = aui.getLiftShaftColour();

                //create ElevatorUI windows for each elevator


                if (sui != null) {
                    sui.dispose();
                }
                if (b != null) {
                    b.dispose();
                }

                if (fui != null) {
                    fui.dispose();
                }
                if (b.eui != null) {
                    for (int i = 0; i < b.euiList.size(); i++) {
                        if (b.euiList.get(i) != null) {
                            b.euiList.get(i).dispose();
                        }
                    }
                }

            }



            setupSim();
            counter++;

        }
        if (ae.getActionCommand().equals("Run Simulation")) {
            b.setAddPersonPrabability(sui.getProbability());
            b.setSimulationTimerSeconds(sui.getSimSeconds());
            new Thread(b).start();
            createFloorUI();
            b.createElevatorUI();

        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new LiftSimulator();
    }
}
